package org.csu.pet_store_2.Controller;


import org.csu.pet_store_2.Common.CommonResponse;
import org.csu.pet_store_2.Domain.Account;
import org.csu.pet_store_2.Domain.Item;
import org.csu.pet_store_2.Domain.Product;
import org.csu.pet_store_2.Service.ProductService;
import org.csu.pet_store_2.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@Transactional
public class ProductController {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductService productService;

    /*
    根据product获取item
    测试成功
    {
    "status": 0,
    "msg": "SUCCESS",
    "data": [
        {
            "itemid": "EST-1",
            "productid": "FI-SW-01",
            "listprice": 16.50,
            "unitcost": 10.00,
            "supplier": 1,
            "status": "P",
            "attr1": "Large",
            "attr2": null,
            "attr3": null,
            "attr4": null,
            "attr5": null
        },
        {
            "itemid": "EST-2",
            "productid": "FI-SW-01",
            "listprice": 16.50,
            "unitcost": 10.00,
            "supplier": 1,
            "status": "P",
            "attr1": "Small",
            "attr2": null,
            "attr3": null,
            "attr4": null,
            "attr5": null
        }
    ]
}
     */
    @GetMapping("/ItemofProduct")
    public CommonResponse<ArrayList<Item>> getItem(@RequestParam("id") String productId)
    {
        ArrayList<Item> itemArrayList=productService.getItemByProductId(productId);
        return CommonResponse.creatForSuccess(itemArrayList);
    }


    /*
    根据productid获取product信息
    测试成功
    {
    "status": 0,
    "msg": "SUCCESS",
    "data": {
        "productid": "FI-SW-01",
        "category": "FISH",
        "name": "Angelfish",
        "descn": "../images/fish1.gif",
        "sum": "78"
        }
        }
     */
    @GetMapping("/Product")
    public CommonResponse<Product> getProduct(@RequestParam("id") String productId)
    {
        Product product=productService.getProductByProductId(productId);
        return CommonResponse.creatForSuccess(product);
    }
}
